   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author MAN
 */
public class Cliente_DAO extends Model implements DaoGenerico {

    public boolean existe(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = getCon().prepareStatement("SELECT COUNT(*) FROM cliente WHERE codigo_cliente =?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public boolean existe1(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sent = "select * from cliente where codigo_cliente like '%" + id.intValue() + "%'";
        try {
            pstmt = getCon().prepareStatement(sent);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public Object load(ResultSet rs) throws SQLException {

        Cliente c = new Cliente();
        c.setCodigo(rs.getInt(1));
        c.setNombre(rs.getString(2));
        c.setTelefono(rs.getString(3));
        c.setEmail(rs.getString(4));
        c.setDireccion(rs.getString(5));
        c.setApellido(rs.getString(6));
        c.setApellido2(rs.getString(7));
        c.setNombre2(rs.getString(8));

        return c;
    }

    public Object extrae(Object o) throws SQLException {
        Integer id = (Integer) o;

        Connection conex = getCon();

        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }
        Cliente c = new Cliente();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = conex.prepareStatement("SELECT * FROM cliente WHERE codigo_cliente=?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                c = (Cliente) load(rs);

            } else {
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }

        return c;
    }

    public void crear(Object o) throws SQLException {
        Cliente c = (Cliente) o;
        Connection conex = getCon();
        Integer id = new Integer(c.getCodigo());
        if (existe(id)) {
            throw new SQLException("Ya existe esta cliente");
        }
        PreparedStatement pstmt = null;

        try {

            pstmt = conex.prepareStatement("insert into cliente values (?,?,?,?,?,?,?,?)");

            pstmt.setInt(1, c.getCodigo());
            pstmt.setString(2, c.getNombre());
            pstmt.setString(3, c.getTelefono());
            pstmt.setString(4, c.getEmail());
            pstmt.setString(5, c.getDireccion());
            pstmt.setString(6, c.getApellido());
            pstmt.setString(7, c.getApellido2());
            pstmt.setString(8, c.getNombre2());


            pstmt.executeUpdate();

        } finally {
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
        }

    }

    public List extrae1(Object cod1) throws SQLException {
        Integer cod = (Integer) cod1;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Cliente c = null;
        List lista = new LinkedList();
        String sent = "select * from cliente where codigo_cliente like '%" + cod.intValue()+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        c = (Cliente) load(rs);
                        lista.add(c);
                    }

        




        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

    
      public List extrae2(Object nom) throws SQLException {
        String  nom1 = (String) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Cliente c = null;
        List lista = new LinkedList();
        String sent = "select * from cliente where nombre like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();
     
            
                    while (rs.next()) {
                        c = (Cliente) load(rs);
                        lista.add(c);
                    }


        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public List extrae3(Object nom) throws SQLException {
        String  nom1 = (String) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Cliente c = null;
        List lista = new LinkedList();
        String sent = "select * from cliente where direccion like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();
        
                    while (rs.next()) {
                        c = (Cliente) load(rs);
                        lista.add(c);
                    }
       




        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }



    public List listar() throws SQLException {
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Cliente c = null;
        List lista = new LinkedList();
        try {
            ps = (PreparedStatement) conex.prepareStatement("select * from cliente ");
            rs = ps.executeQuery();

            while (rs.next()) {
                c = (Cliente) load(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

    public boolean update(Cliente c) throws SQLException {
        Connection conex = getCon();
        Integer id = new Integer(c.getCodigo());

        if (!existe(id)) {
            throw new SQLException("Cliente no registrado " + c.getCodigo());
        }
        PreparedStatement pstmt = null;
        boolean res = false;
        try {
            pstmt = conex.prepareStatement("Update cliente set codigo_cliente  = ?, nombre = ?, telefono=?, email=?, direcion=?, apellido  = ?, segundoapellido = ?,segundonombre  = ?  where codigo_cliente = ?");
            pstmt.setInt(1, c.getCodigo());
            pstmt.setString(2, c.getNombre());
            pstmt.setString(3, c.getTelefono());
            pstmt.setString(4, c.getEmail());
            pstmt.setString(5, c.getDireccion());
            pstmt.setString(7, c.getApellido());
            pstmt.setString(8, c.getApellido2());
            pstmt.setString(6, c.getNombre2());
            pstmt.executeUpdate();




            int r = pstmt.executeUpdate();
            if (r != 0) {
                res = true;
            }
        } finally {
            if (pstmt != null) {
                pstmt.close();
            }
        }
        return res;
    }

      private Model model;
    public Cliente_DAO() throws SQLException {
        model = Model.getInstance();
    }
    public Cliente find(Integer id) throws SQLException {
                  Connection conex=Model.getCon();

                if (!model.isConnect())
			throw new SQLException("no hay conexcion");
                Cliente c=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM cliente WHERE codigo_cliente=?");
                            System.out.println(id.intValue());
                        pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){

                      c=(Cliente)load(rs);
                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return c;
    }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listarlista(List l) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
